package com.example.hidesms;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.example.hidesms.adapter.HomePageAdapter;
import com.example.hidesms.bo.SmsBO;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.receiver.SmsFilter;
import com.example.hidesms.sqlite.DataBaseHelper;
import com.example.hidesms.utils.Utils;

public class HomePageFragment extends Fragment {
	
	private GridView gridView;
	private ArrayList<SMSItem> lstSMS;
	private HomePageAdapter mAdapter;
	private ProgressBar mProgressBar;
	private SmsFilter smsFilter;
	private HomeListener homeListener;

	
	public void setHomeListener(HomeListener homeListener) {
		this.homeListener = homeListener;
	}

	public interface HomeListener extends Parcelable{
		public void startCate(String address);
		
		public void addListBlock();
		public void addListBlockIncoming();
		public void addListBlockOutgoing();
		public void addListCoolSms();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.home_screen, container, false);
		getElementFromUI(view);
		return view;
	}
	
	private void getElementFromUI(View view){
		gridView = (GridView) view.findViewById(R.id.list);
		mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
		if (smsFilter ==null ) {
			smsFilter = new SmsFilter();
			getActivity().registerReceiver(smsFilter, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
		}
		getData();
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> adapter, View arg1, int pos, long arg3) {
				ArrayAdapter<SMSItem> item = (ArrayAdapter<SMSItem>) adapter.getAdapter();
				String str = item.getItem(pos).getAddressNameS();
				homeListener.startCate(str);
			}
		});
		
	}
	
	private void getData(){
		mProgressBar.setVisibility(View.VISIBLE);
		File dbFile = getActivity().getDatabasePath("PrivaceSms.db"); 
		Log.e("getdata"," path "+dbFile.getAbsolutePath());
		Thread th = new Thread(new Runnable() {
			
			@Override
			public void run() {
				DataBaseHelper myDatabase = new DataBaseHelper(getActivity());
				lstSMS = SmsBO.getInstance().getAll(myDatabase, null, true);
				if (lstSMS.size()<=0) {
					lstSMS = Utils.getAllSms(getActivity());
					lstSMS = SmsBO.getInstance().getAll(myDatabase, null, true);
				}
//				lstSMS = SmsBO.getInstance().getByAdress(myDatabase, "MuACHUNG");
//				Collections.sort(lstSMS, new Comparator<SMSItem>(){
//					@Override
//					public int compare(SMSItem s1, SMSItem s2) {
//						return s1.getTimeOr().compareToIgnoreCase(s2.getTimeOr());
//					}
//
//			    });
				myDatabase.close();
				Log.e("getdata", "<0 "+lstSMS.size());
				
				if (lstSMS.size()>0) {
					getActivity().runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							if (mAdapter == null) {
								mAdapter = new HomePageAdapter(getActivity(), -1, lstSMS);
//								mAdapter.setHomeListener(homeListener);
								gridView.setAdapter(mAdapter);
							}else{
								mAdapter.notifyDataSetChanged();
							}
							mProgressBar.setVisibility(View.GONE);
						}
					});
				}
				
			}
		});
		th.start();
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, 123, 1, "fuck");
		menu.add(1, 321, 3, "fuck");
	}
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		SubMenu sub = menu.addSubMenu("Group");//.setIcon(R.drawable.tab_selected);
		sub.add(0, R.style.AppTheme, 0, "Date");
		sub.add(0, R.style.AppTheme, 0, "Phone");
//		sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		SubMenu sub1 = menu.addSubMenu("Filter");//.setIcon(R.drawable.group_selected);
		sub1.add(0, R.style.AppTheme, 0, "Default");
		sub1.add(0, R.style.AppTheme, 0, "Light");
//		sub1.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (smsFilter!=null) {
			getActivity().unregisterReceiver(smsFilter);
			smsFilter = null;
		}
	}
	
	private void getSMS() {
		ArrayList<SMSItem> lstSMS = Utils.getAllSms(getActivity());
		String msgData = "";
		for (int i = 0; i < lstSMS.size(); i++) {
			msgData += "Content: " + lstSMS.get(i).getMsg();
		}
		Log.e("getSMS", " " + msgData);
	}

}
